package com.android.meige.fragment.member;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.android.meige.R;
import com.android.meige.activity.BaseObject;
import com.android.meige.activity.mine.RefundDetailActivity;
import com.android.meige.adapt.listMineOrderAdapt;
import com.android.meige.model.data;
import com.android.meige.model.dataCallback;
import com.android.meige.model.detail.PayMent;
import com.android.meige.model.detail.PayMentCallback;
import com.android.meige.model.detail.paydone;
import com.android.meige.model.detail.paydoneCallback;
import com.android.meige.model.detail.paydonetoshare;
import com.android.meige.model.detail.paydonetoshareCallback;
import com.android.meige.model.detail.wchatpaydone;
import com.android.meige.model.detail.wchatpaydoneCallback;
import com.android.meige.model.member.OrderListData;
import com.android.meige.model.member.OrderListDataCallback;
import com.android.meige.utils.ConstantsUrl;
import com.android.meige.utils.HttpUtils;
import com.android.meige.utils.LoginState;
import com.android.meige.utils.MyLog;
import com.android.meige.utils.ObjectUtils;
import com.android.meige.utils.alipay.PayResult;
import com.android.meige.view.dialog.MainMsg;
import com.android.meige.view.group.VRefreshLayout;
import com.android.meige.view.recycleview.CustomLoadMoreView;
import com.android.meige.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 订单列表
 * ---------------------------------------
 */
public class Fragment_Mine_Order extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    VRefreshLayout mRefreshLayout;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;

    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress_loading)
    FrameLayout progressLoading;
    private View rootView;
    private static final String ARG_PARAM1 = "param1";
    private PopupWindow mPopWindow;

    private String mParam1;
    private String order_sn;
    private String pay_code;
    /**
     * 微信注册类
     */
    private String common_id = "";
    private IWXAPI iwxapi;
    private String orderInfo;
    private boolean isReallyOrder_flag;
    private static final int SDK_PAY_FLAG = 1;
    //控件是否已经初始化
    private boolean isPrepared;
    private listMineOrderAdapt adapter;

    private int page = 1;
    private boolean networkConnected;
    private FragmentActivity mcontext;
    private int pospay = 0;
    private PayMentAdapt payAdapter;
    private int pay_id;

    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        Toast.makeText(mcontext, "支付成功", Toast.LENGTH_SHORT).show();
                        GetToSharePage();
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        if (TextUtils.equals(resultStatus, "8000") || TextUtils.equals(resultStatus, "6004")) {
                            ObjectUtils.toast(mcontext, "支付结果确认中");
                        }
                        else if (TextUtils.equals(resultStatus, "6001")) {
                            ObjectUtils.toast(mcontext, "支付取消");
                        }
                        else if (TextUtils.equals(resultStatus, "6002")) {
                            ObjectUtils.toast(mcontext, "网络异常");
                        }
                        else if (TextUtils.equals(resultStatus, "5000")) {
                            ObjectUtils.toast(mcontext, "重复请求");
                        }
                        else {
                            // 其他值就可以判断为支付失败
                            ObjectUtils.toast(mcontext, "支付失败");
                        }
                    }
                    break;
                }
                default:
                    break;
            }
        };
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    public static Fragment_Mine_Order newInstance(String param1) {
        Fragment_Mine_Order fragment = new Fragment_Mine_Order();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        rootView = inflater.inflate(R.layout.layout_recycle_refresh, container, false);
        ButterKnife.bind(this, rootView);
        mcontext = getActivity();
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        mainTopBg.setVisibility(View.GONE);
        initUI();
        isPrepared = true;
        setlazyLoad();
        return rootView;
    }

    @Override
    protected void setlazyLoad() {
        super.setlazyLoad();
        if (!isPrepared || !isVisible) {
            return;
        }
        page = 1;
        mRecyclerView.scrollToPosition(0);
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            GetDataListData(page, mParam1);
            if (mParam1.equals("0") || mParam1.equals("100")) {
                getPayMent();
            }
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
        //刷新
        ReFreshData();
    }

    private void ReFreshData() {
        if (mRefreshLayout != null) {
            mRefreshLayout.setBackgroundColor(Color.TRANSPARENT);
            mRefreshLayout.setAutoRefreshDuration(400);
            mRefreshLayout.setRatioOfHeaderHeightToReach(1.5f);
            mRefreshLayout.addOnRefreshListener(new VRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    networkConnected = ObjectUtils.isNetworkConnected(mcontext);
                    if (networkConnected) {
                        mRefreshLayout.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                page = 1;
                                GetDataListData(page, mParam1);
                                mRefreshLayout.refreshComplete();
                            }
                        }, 1000);
                    } else {
                        ObjectUtils.toast(mcontext, "网络连接出现异常");
                    }
                }
            });
        }
    }

    private void initUI() {
        adapter = new listMineOrderAdapt(R.layout.item_list_mine_order, null);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        final LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.GetToTop((ImageView) rootView.findViewById(R.id.top), 0);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                Intent intent = new Intent(mcontext, BaseObject.class);
                intent.putExtra("title", "订单详情");
                intent.putExtra("need_id", adapter.getItem(position).getOrder_sn());
                mcontext.startActivity(intent);
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public boolean onItemChildClick(BaseQuickAdapter adapter1, View view, final int position) {
                final MainMsg mainMsg;
                final OrderListData.MineOrderdata.MineOrderlistdata adapterItem = adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.daipinjia_pinjia:
                        Intent intent_rate = new Intent(mcontext, BaseObject.class);
                        intent_rate.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "评价");
                        intent_rate.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_GOOD_ID, adapterItem.getGood_id());
                        intent_rate.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_ID, adapterItem.getId());
                        intent_rate.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_BUY_NUM, adapterItem.getBuy_num());
                        intent_rate.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_GOODS_SPEC, adapterItem.getGoods_spec());
                        intent_rate.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_RATE_THUMB, adapterItem.getThumb());
                        startActivity(intent_rate);
                        break;
                    case R.id.daipinjia_wuliu:
                        Intent intent_refound = new Intent(mcontext, RefundDetailActivity.class);
                        intent_refound.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_ID, adapterItem.getId() + "");
                        intent_refound.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_REFOUND_TYPE, 2);
                        startActivity(intent_refound);
                        break;
                    case R.id.daishouhuo_daifahuo_detail:
                        Intent intent = new Intent(mcontext, BaseObject.class);
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "订单详情");
                        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID, adapterItem.getOrder_sn());
                        startActivity(intent);
                        break;
                    case R.id.daishouhuo_daifahuo_lianximaijia:
//                        ObjectUtils.toast(mcontext, "功能开发中");
                        OrderListData.MineOrderdata.MineOrderlistdata item = adapterItem;
                        String kf_online = item.getKf_online();
                        String good_id = item.getGood_id();
                        Intent intents = new Intent(mcontext, BaseObject.class);
                        intents.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "客服");
                        if (TextUtils.isEmpty(kf_online)) {
                            intents.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL, ConstantsUrl.baseDomain + "chat?good_id=" + good_id);
                        } else {
                            intents.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL, kf_online);
                        }
                        startActivity(intents);
                        break;
                    case R.id.daishouhuo_wuliu:
                        Intent intent_express = new Intent(mcontext, BaseObject.class);
                        intent_express.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "物流详情");
                        intent_express.putExtra("need_id", adapterItem.getOrder_sn());
                        startActivity(intent_express);
                        break;
                    case R.id.daishouhuo_queren:
                        mainMsg = new MainMsg(mcontext);
                        mainMsg.show(mcontext.getWindow().getDecorView(), 0, "是否确认收到货物？");
                        mainMsg.setOnSetSureClickListener(new MainMsg.OnSetSureClickListener() {
                            @Override
                            public void onSure() {
                                String address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDERQUERENSHOUHUO + "?id=" + adapterItem.getId();
                                setCancel(address, position);
                                mainMsg.close();
                            }
                        });
                        break;
                    case R.id.daifukuang_pay:
                        if (adapterItem.getOrder_sn() != null) {
                            order_sn = adapterItem.getOrder_sn();
                            showPopupWindow();
                        }
//                        pay_code = adapterItem.getPay_code();
//                        MyLog.e("pay_code", pay_code + "");
//                        GetToPay();
                        // ObjectUtils.toast(mcontext, "抱歉，支付方式还未启用");
                        break;
                    case R.id.daifukuang_canceldingdang:
                        mainMsg = new MainMsg(mcontext);
                        mainMsg.show(mcontext.getWindow().getDecorView(), 0, "是否取消订单？");
                        mainMsg.setOnSetSureClickListener(new MainMsg.OnSetSureClickListener() {
                            @Override
                            public void onSure() {
                                String address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDERCANCEL + "?id=" + adapterItem.getId();
                                setCancel(address, position);
                                mainMsg.close();
                            }
                        });
                        break;
                    case R.id.item_list_mine_dingdang_lottery_result:
                        Intent intent_result = new Intent(mcontext, BaseObject.class);
                        intent_result.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "试用结果");
                        intent_result.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID, adapterItem.getGood_id());
                        startActivity(intent_result);
                        break;
                    case R.id.daipinjia_detele:
                        mainMsg = new MainMsg(mcontext);
                        mainMsg.show(mcontext.getWindow().getDecorView(), 0, "是否删除订单？");
                        mainMsg.setOnSetSureClickListener(new MainMsg.OnSetSureClickListener() {
                            @Override
                            public void onSure() {
                                String address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDERDETELE + "?id=" + adapterItem.getId();
                                setCancel(address, position);
                                mainMsg.close();
                            }
                        });
                        break;
                }
                return false;
            }
        });
        payAdapter = new PayMentAdapt(R.layout.item_pay, null);
        payAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                PayMent.DataBean.PaymentBean paymentBean = payAdapter.getItem(position);
                pospay = position;
                payAdapter.notifyDataSetChanged();
                pay_id = paymentBean.getPay_id();
                pay_code = paymentBean.getPay_code();
                mPopWindow.dismiss();
                GetToPay();
            }
        });
        initPopupWindow();
    }

    private void getPayMent() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBER_PAY_MENT ;
            HttpUtils.get(address,  null, new PayMentCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.toast(mcontext, "没有获取到支付方式");
                }

                @Override
                public void onResponse(PayMent response, int id) {
                    if (response == null) {
                        ObjectUtils.toast(mcontext, "没有获取到支付方式");
                        return;
                    }
                    if (response.getCode() == 0 && response.getData() != null) {
                        List<PayMent.DataBean.PaymentBean> payment = response.getData().getPayment();
                        if(ObjectUtils.listHasData(payment)){
                            payAdapter.setNewData(payment);
                        }
                    }
                }
            });
        }
    }

    private void showPopupWindow() {
        //显示PopupWindow
        mPopWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
    }

    private void initPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(mcontext).inflate(R.layout.popup_payment, null);
        mPopWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        CustomRecycleView mRecyclerView = (CustomRecycleView)contentView.findViewById(R.id.listView);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(payAdapter);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopWindow.setOutsideTouchable(true);
    }

    //生成订单以及付款
    private void GetToPay() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            progressLoading.setVisibility(View.VISIBLE);
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBER_DETAILORDER_PAY;
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            HashMap<String, String> header = new HashMap<>();
            header.put("UID", getid);
            header.put("UPSW", getpd);
            HashMap<String, String> map = new HashMap<>();
            map.put("order_sn", order_sn);
            map.put("pay_id", pay_id+"");
            MyLog.e("order_snddweixin", order_sn.toString());

            if (pay_code.equals("alipayapp")) {
                HttpUtils.get(address, header, map, new paydoneCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressLoading.setVisibility(View.GONE);
                        getActivity().finish();
                    }

                    @Override
                    public void onResponse(paydone response, int id) {
                        if (response == null) {
                            progressLoading.setVisibility(View.GONE);
                            return;
                        }
                        paydone.DataBean data = response.getData();
                        if (response.isFlag() && data != null) {
                            isReallyOrder_flag = response.isFlag();
                            if (isReallyOrder_flag) {
                                if (data.getPayment() != null) {
                                    orderInfo = data.getPayment();
                                    //支付宝支付发起
                                    Runnable payRunnable = new Runnable() {
                                        @Override
                                        public void run() {
                                            PayTask alipay = new PayTask(mcontext);
                                            Map<String, String> result = alipay.payV2(orderInfo, true);
                                            Message msg = new Message();
                                            msg.what = SDK_PAY_FLAG;
                                            msg.obj = result;
                                            mHandler.sendMessage(msg);
                                        }
                                    };
                                    Thread payThread = new Thread(payRunnable);
                                    payThread.start();
                                } else {
                                    String common_id = data.getCommon_id();
                                    if(!TextUtils.isEmpty(common_id)){
                                        toTeamDetail(common_id);
                                    }
                                }
                                progressLoading.setVisibility(View.GONE);
                            }
                        } else {
//                            ObjectUtils.toast(mcontext, response.getMsg());
                            Toast.makeText(mcontext, response.getMsg(), Toast.LENGTH_LONG).show();
                            progressLoading.setVisibility(View.GONE);
                            return;
                        }
                    }
                });
            } else {
                HttpUtils.get(address, header, map, new wchatpaydoneCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        progressLoading.setVisibility(View.GONE);
                        getActivity().finish();
                    }

                    @Override
                    public void onResponse(wchatpaydone response, int id) {
                        if (response == null) {
                            progressLoading.setVisibility(View.GONE);
                            return;
                        }
                        wchatpaydone.DataBean data = response.getData();
                        if (response.isFlag() && data != null) {
                            isReallyOrder_flag = response.isFlag();
                            if (isReallyOrder_flag) {
                                wchatpaydone.PaymentBean payment = data.getPayment();
                                if (payment != null) {
                                    //微信支付发起
                                    iwxapi = WXAPIFactory.createWXAPI(mcontext, ConstantsUrl.WX_APPID, true);
                                    iwxapi.registerApp(ConstantsUrl.WX_APPID);
                                    PayReq req = new PayReq();
                                    req.appId = ConstantsUrl.WX_APPID;
                                    req.partnerId = payment.getPartnerid();
                                    req.prepayId = payment.getPrepayid();
                                    req.packageValue = payment.getWxpackage();
                                    req.nonceStr = payment.getNoncestr();
                                    req.timeStamp = payment.getTimestamp();
                                    req.sign = payment.getSign();
                                    iwxapi.sendReq(req);
                                }
                            } else {
                                String common_id = data.getCommon_id();
                                if(!TextUtils.isEmpty(common_id)){
                                    toTeamDetail(common_id);
                                }
                            }
//                            Timer timer = new Timer();// 实例化Timer类
//                            timer.schedule(new TimerTask() {
//                                public void run() {
//                                    mcontext.finish();
//                                }
//                            }, 3000);// 这里百毫秒
                            progressLoading.setVisibility(View.GONE);
                        } else {
//                            ObjectUtils.toast(mcontext, response.getMsg());
                            Toast.makeText(mcontext, response.getMsg(), Toast.LENGTH_LONG).show();
                            progressLoading.setVisibility(View.GONE);
                            return;
                        }
                    }
                });
            }
        } else {
            ObjectUtils.toast(mcontext, "网络未连接");
        }
    }

    private void toTeamDetail(String common_id) {
        Intent intent_tuan = new Intent(mcontext, BaseObject.class);
        intent_tuan.putExtra("title", "团详情");
        intent_tuan.putExtra("need_id", common_id);
        mcontext.startActivity(intent_tuan);
    }

    //支付成功去分享页面
    private void GetToSharePage() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            progressLoading.setVisibility(View.VISIBLE);
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBERPAYDONETOShare;
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            HashMap<String, String> header = new HashMap<>();
            header.put("UID", getid);
            header.put("UPSW", getpd);
            HashMap<String, String> map = new HashMap<>();
            map.put("order_sn", order_sn);
            map.put("pay_id", pay_id+"");
            MyLog.e("", order_sn.toString());
            HttpUtils.get(address, header, map, new paydonetoshareCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    progressLoading.setVisibility(View.GONE);
                    getActivity().finish();
                }

                @Override
                public void onResponse(paydonetoshare response, int id) {
                    if (response == null) {
                        progressLoading.setVisibility(View.GONE);
                        return;
                    }
                    paydonetoshare.DataBean data = response.getData();
                    if (data != null) {
                        common_id = data.getCommon_id();
                        if (Integer.parseInt(common_id) > 0) {
                            Intent intent_tuan = new Intent(mcontext, BaseObject.class);
                            intent_tuan.putExtra("title", "团详情");
                            intent_tuan.putExtra("need_id", common_id);
                            mcontext.startActivity(intent_tuan);
                        } else {

                        }
                        progressLoading.setVisibility(View.VISIBLE);
                        getActivity().finish();
                    }
                }
            });
        } else {
            ObjectUtils.toast(mcontext, "网络未连接");
        }
    }

    //获取列表数据
    private void GetDataListData(final int page, String type) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address;
            if (type.equals("0")) {
                address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDER + "?page=" + page;
            } else if (type.equals("110")) {
                //待评价
                address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDER + "?page=" + page + "&status=" + type + "&is_rate=0";
            } else {
                address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDER + "?page=" + page + "&status=" + type;
            }
            MyLog.e("地址", address);
            HashMap<String, String> header = new HashMap<>();
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            header.put("UID", getid);
            header.put("UPSW", getpd);

            HttpUtils.get(address, header, null, new OrderListDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(OrderListData response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        OrderListData.MineOrderdata data = response.getData();
                        if (data == null || !ObjectUtils.listHasData(data.getList())) {
                            if (page == 1) {
                                noData.setVisibility(View.VISIBLE);
                            } else {
                                adapter.loadMoreEnd();
                            }
                            return;
                        }
                        noData.setVisibility(View.GONE);
                        ArrayList<OrderListData.MineOrderdata.MineOrderlistdata> listdata = new ArrayList<>();
                        List<OrderListData.MineOrderdata.MineOrderlistdata> list = data.getList();
                        listdata.addAll(list);
                        if (page == 1) {
                            adapter.setNewData(listdata);
                        } else {
                            adapter.addData(listdata);
                            adapter.loadMoreComplete();
                        }
                    } else {
                        if (page == 1) {
                            noData.setVisibility(View.VISIBLE);
                        } else {
                            adapter.loadMoreFail();
                            ObjectUtils.toast(mcontext, response.getMsg());
                        }
                    }
                }
            });
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络连接出现异常");
            adapter.loadMoreFail();
        }
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        page = 1;
        GetDataListData(page, mParam1);
        if (mParam1.equals("0") || mParam1.equals("100")) {
            getPayMent();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        GetDataListData(page, mParam1);
    }

    private void setCancel(String url, final int position) {
        progressLoading.setVisibility(View.VISIBLE);
        String getid = LoginState.getInstance().getid(mcontext);
        String getpd = LoginState.getInstance().getpd(mcontext);
        HashMap<String, String> header = new HashMap<>();
        header.put("UID", getid);
        header.put("UPSW", getpd);
        HttpUtils.get(url, header, null, new dataCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {
                progressLoading.setVisibility(View.GONE);
            }

            @Override
            public void onResponse(data response, int id) {
                progressLoading.setVisibility(View.GONE);
                if (response == null) {
                    return;
                }
                ObjectUtils.toast(mcontext, response.getMsg());
                if (response.getCode() == 0) {
                    adapter.remove(position);
                    if (adapter.getData().size() == 0) {
                        noData.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    private class PayMentAdapt extends BaseQuickAdapter<PayMent.DataBean.PaymentBean, BaseViewHolder> {

        public PayMentAdapt(int layoutResId, List<PayMent.DataBean.PaymentBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, PayMent.DataBean.PaymentBean item) {
            helper.setText(R.id.pay_name, item.getPay_name());
            ObjectUtils.photo2(mcontext, item.getThumb(), (ImageView) helper.getView(R.id.pay_logo));
            int position = helper.getAdapterPosition();
            if (pospay == position) {//最后选择的位置
                helper.setChecked(R.id.item_list_mine_pay_check, true);
            } else {
                helper.setChecked(R.id.item_list_mine_pay_check, false);
            }
        }
    }
}
